﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.LocLabels;

namespace RibbonEditor.Controls.LocLabels
{
    public partial class TitleControl : UserControl, IControlRulable
    {
        private Title title;

        internal Title Title
        {
            get { return title; }
            set { title = value; }
        }

        public TitleControl()
        {
            InitializeComponent();
            this.title = new Title();
        }

        public TitleControl(Title title)
            : this()
        {
            this.title = title;
            this.FillControls();
        }

        public void FillControls()
        {
            try
            {
                if (this.title != null && this.title.LanguageCode != null)
                {
                    this.scTitle.Value = this.Title.Description;
                    this.icLCID.Value = int.Parse(this.Title.LanguageCode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                this.Title.Description = this.scTitle.Value;
                this.Title.LanguageCode = this.icLCID.Value.Value.ToString();

                UserControlHelper.CleanDirtyFields(this);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members

        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.title.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
